package com.flying.carclub;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.flying.carclub.activity.LoginActivity;
import com.flying.carclub.fragment.DiscoveryFragment;
import com.flying.carclub.fragment.HomeFragment;
import com.flying.carclub.fragment.MineFragment;
import com.flying.carclub.fragment.ShowFragment;
import com.flying.carclub.utils.BottomNavigationViewHelper;
import com.mob.MobSDK;
import com.review.signature.Review;
import com.stonesun.newssdk.NewsAgent;
import com.stonesun.newssdk.fragment.NewsAFragment;

import java.util.ArrayList;

import cn.newssdk.share.Share;

public class MainActivity extends BaseActivity {

    HomeFragment homeFragment = new HomeFragment();
//    DiscoveryFragment discoveryFragment = new DiscoveryFragment();
    ShowFragment showFragment = new ShowFragment();
    MineFragment mineFragment = new MineFragment();
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private Fragment lastFragment = homeFragment;
    public final static int FRAGMENT_CONTAINER_ID = R.id.frame_container;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);




        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        BottomNavigationViewHelper.disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        showFragment(homeFragment);
    }

    private void showFragment(Fragment targetFragment) {
        if (!targetFragment.isVisible()) {
            removeAllTopFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            if (!fragments.contains(targetFragment)) {
                fragments.add(targetFragment);
                fragmentTransaction.add(FRAGMENT_CONTAINER_ID, targetFragment);
            }
            fragmentTransaction.hide(lastFragment);
            fragmentTransaction.show(targetFragment);
            fragmentTransaction.commitAllowingStateLoss();
            lastFragment = targetFragment;
        }
    }
    public void removeAllTopFragment() {
        // TODO Auto-generated method stub
        int count = getSupportFragmentManager().getBackStackEntryCount();
        for (int i = 0; i < count; i++) {
            getSupportFragmentManager().popBackStackImmediate();
        }
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    removeAllTopFragment();
                    showFragment(homeFragment);
                    return true;
                /*case R.id.navigation_discovery:
                    removeAllTopFragment();
                    showFragment(discoveryFragment);
                    return true;*/
                case R.id.navigation_dashboard:
                    removeAllTopFragment();
                    showFragment(showFragment);
                    return true;
                case R.id.navigation_notifications:
                    removeAllTopFragment();
                    showFragment(mineFragment);
                    return true;
            }
            return false;
        }
    };
    // 如果不做任何处理，浏览网页，点击系统“Back”键，整个Browser会调用finish()而结束自身，
    // 如果希望浏览的网 页回退而不是推出浏览器，需要在当前Activity中处理并消费掉该Back事件。
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;

        }
        return super.onKeyDown(keyCode, event);
    }

    //退出时的时间
    private long mExitTime;

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            showToast( "再按一次退出");
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

}
